package com.itelite.config;

import com.itelite.domain.dto.UserVo;
import com.itelite.domain.pojo.Config;
import com.itelite.domain.pojo.Group;
import com.itelite.mapper.ConfigMapper;
import com.itelite.mapper.GroupMapper;
import com.itelite.mapper.ValidateMapper;
import com.itelite.service.GroupService;
import com.itelite.service.impl.SchedulerServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * @author 沈洋 邮箱:1845973183@qq.com
 * @create 25-06-2021-13:45
 **/
@Configuration
@Slf4j
public class ActiveScheduler {
    @Value("${spring.mail.username}")
    private String from = "1185914928@qq.com";
    @Value("${front.server.ip}")
    private String frontServerIp = "relosy.cn";
    @Autowired
    private ConfigMapper configMapper;
    @Autowired
    private SchedulerServiceImpl schedulerService;
    @Autowired
    private ValidateMapper validateMapper;
    @Autowired
    private GroupMapper groupMapper;
    @Scheduled(cron="0 30 12 ? * SUN")
    public void process1(){
        Config config  = configMapper.queryConfig(4);
        if(config==null){
            log.info("邮件发送失败：全局配置未开启");
            return;
        }
        List<Group> groups = groupMapper.queryGroupForEmail();
        if(!CollectionUtils.isEmpty(groups)) {
            groups.forEach(g -> {
                g.getLeaders().forEach(l -> {
                    SimpleMailMessage mail = new SimpleMailMessage();
                    mail.setFrom(from);
                    mail.setTo("1845973183@qq.com");
                    mail.setSubject("【IT培优】");
                    StringBuilder text = new StringBuilder();
                    if (l.getIdentity() != null) {
                        text.append(l.getIdentity()).append("您好：");
                    }
                    if (g.getGroupName() != null) {
                        text.append(g.getGroupName());
                    }
                    text.append("本周还未上传培训记录，请前往 http://").append(frontServerIp)
                            .append(" 提交本周培训记录。（若本周无培训则请忽略本邮件）");
                    mail.setText(text.toString());
                    schedulerService.sendEmail(mail);
                    log.info(g.getGroupName() + "小组组长-" + l.getRealName() + "邮件发送成功！！");
                });
            });
        }
    }

    /**
     * 清理重置请求
     */
    @Scheduled(cron="0 0 0 L * ?")
    public void process2(){
        int day = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
        log.info("定时清除近"+day+"天的重置密码/邮箱请求");
        int row = validateMapper.deleteValidate(day);
        log.info("共清除"+row+"个重置请求数据");
    }
}
